package com.example.workone;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class UC3 extends AppCompatActivity {
    private int currentimage = 0;
    //定义一个图像数组
    final int[] images = new int[] {
            R.drawable.flower1,
            R.drawable.flower2,
            R.drawable.flower3,
            R.drawable.flower4,
            R.drawable.flower5
    };
    final int[] names = new int[] {
            R.id.view1,
            R.id.view2,
            R.id.view3,
            R.id.view4,
            R.id.view5
    };
    //声明用到的5个imageview组件
    ImageView[] views = new ImageView[names.length];

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //表面消息来自于本程序所发
            if (msg.what == 0x123)
            {
                for(int i=0;i<names.length;i++)
                {
                    //每帧的背景做一次变换
                    views[i].setImageResource(images[(i+currentimage) % names.length]);
                }
                currentimage ++;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc3);
        //找到ImageView对应的组件
        for(int i=0;i<names.length;i++)
        {
            views[i] = (ImageView) findViewById(names[i]);
        }
        //计时器 2秒发送一次信息
        new Timer().schedule(new TimerTask() {
            @Override
            public void run ()
            {
                //发送一条空消息通知系统改变5个Imageview组件的背景图片
                handler.sendEmptyMessage(0x123);
            }
        },0,2000);

    }
}
